// pages/detail/detail.js
const app = getApp()
var goodsid=0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX: app.globalData.isIpx,
    imgBanners: [],
    isBuyCar:true,            //是否点击的购物车
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 500,

    //优惠券id
    couponid:'',
    // 商品名称
    goodsName:'',
    moeny:'0',
    yunfei:'包邮',
    shopdes:'',
    detail:[],
    yuanjia:0,                        //原价
    // 遮罩层
    tocar:{ img:'',name:'',money: ''},
    datanum: 1,       //加减商品数量
    kuanshitype: [{name:'',val:''},{name:'', val: ''}],
    inventory:0,        //库存
    checkId:0,
    bg:'',    
    // 是否显示
    showView: false,
    kuanshi:false,
    lookNum:0,         //浏览次数
    zanNum:0,
    zantype: false,
    zanstate: '/pages/detail/icon/icon_def.png',
    zanHeads: [],
    myvideo:"",
    videoHide:true,
    text_sign:"",
    timegoodsid:'',       //接口需要
    huodong:''
  },

  kuanshibin: function (e) {
    this.setData({
      kuanshi: (!this.data.kuanshi)
    })
  },
  //加
  jia:function () {
    var datanums =this.data.datanum;
    if (datanums >= this.data.inventory){
       wx.showToast({
         title: '你太丑了',
       }) 
    }else{
      this.setData({
        datanum: datanums + 1
      }) 
    }
     
  },
  // 减
  jian: function () {
    var datanum =this.data.datanum-1;
    if (datanum<1){
      datanum=0;
    }
   
    this.setData({
      datanum: datanum
    })
  },

  //遮罩层点击显示隐藏   加入购物车
  toCar: function (e) {
    this.setData({
      isBuyCar: true,
      showView: (!this.data.showView)
    })
  },

  // 立即购买
  nowBuy: function () {
    this.setData({
      isBuyCar: false,
      showView: (!this.data.showView)
    })
  },

  //添加购物车成功
  next:function(){
    var self=this;
    if (self.data.datanum==0){
      wx.showToast({
        title: '不能添加0件商品',
        icon: 'none',
      })
      return;
    }
    // 商品数量
    var num = this.data.datanum     
    if (this.data.isBuyCar){
      //加入购物车
      wx.request({
        url: app.serverUrl + 'addCar',      //请求的地址
        method: 'post',
        data: {
          openid: app.openid,
          goodsid: goodsid,
          goodsnumber: num,
          timegoodsid: self.data.timegoodsid
        },
        success: function (res) {
          self.setData({
            showView: (!self.data.showView)
          })
          if (res.data.flag === 'success') {
            wx.showLoading({
              title: '正在加购~',
              mask: true,
              success: function () {
                wx.showToast({
                  title: '加购成功',
                  duration: 2000,
                  mask: true,
                })
              }
            })
          }
        }
      })  
    }else{
      self.setData({
        showView: (!self.data.showView)
      })
      //立即购买
      var type = 'formShop'
      wx.navigateTo({

        url: '/pages/payorder/payorder?type=' + type + '&num=' + num + '&goodsid=' + goodsid + '&timegoodsid=' + self.data.timegoodsid
      
      })
    }
  },
 
  // 样式选择
  radioChange: function (e) {
    this.setData({
      checkId:e.detail.value,
      bg:'#0bb908'
    })
   
  },

  // 跳转购物车
  toShopCar:function(){
    wx.navigateTo({
      url: '/pages/shopcar/shopcar',
    })
  },


  //显示图片预览图
  shopImgShow:function(e){
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.detail[index],
      urls: this.data.detail
    })
  },

  // 底部轮播图页面展示
  bannerImg:function(e){
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.imgBanners[index],
      urls: this.data.imgBanners
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '商品详情'
    })
    // wx.showLoading({
    //   title: '正在加载...',
    // })
    var self = this;
    
    showView: (options.showView == "true" ? true : false)
    goodsid= options.goodsid;
    var ishuodong = options.huodong;
    self.setData({
      huodong:options.huodong
    })
    
    console.log(options)  
    wx.request({
      url: app.serverUrl +'goodsDetail',      //请求的地址
      method: 'post',
      data: {
        goodsid: goodsid,
        openid: app.openid,
        huodong: ishuodong
      },   
      success: function (res) {   
        // tocar: { img: '/images/1.jpg', name:'古玩虫 玉佩', money: '66.66' },
        console.log('商品详情',res);
        self.setData({
          imgBanners: res.data.data.goodsbanner,
          goodsName: res.data.data.goodsname,
          moeny: res.data.data.goodsmoney,
          yunfei: res.data.data.goodsfreight,
          detail: res.data.data.goodsimages,
          tocar: { img: res.data.data.goodsbanner[0], name: res.data.data.goodsname, money: res.data.data.goodsmoney },
          couponid: res.data.data.couponid,
          shopdes: res.data.data.goodstitle,
          inventory: res.data.data.inventory,
          lookNum: res.data.data.liulan,
          zantype: res.data.data.zantype,
          zanNum: res.data.data.zancount,
          myvideo: res.data.data.video,
          text_sign:res.data.data.zhu,
          yuanjia: res.data.data.yuanjia,
          timegoodsid: res.data.data.timegoodsid
        })

        if (self.data.zantype) {
          self.setData({
            zanstate: '/pages/detail/icon/icon_press.png'
          })
        } else {
          self.setData({
            zanstate: '/pages/detail/icon/icon_def.png'
          })
        }

        if (res.data.data.inventory<1){
          self.setData({
            datanum:0
          })
        }
        wx.hideLoading()
      }
    })
  },
  // 点赞
  dianzan: function () {
    var self = this;
    var url = app.serverUrl + 'goodsZan'
    if (!this.data.zantype) {
      wx.request({
        url: url,
        method: 'post',
        data: {
          goodsid: goodsid,
          opeind: app.openid
        },
        success: function (res) {
          self.setData({
            zanstate: '/pages/detail/icon/icon_press.png',
            zantype: true,
            zanNum: self.data.zanNum + 1
          })
          wx.showToast({
            title: '点赞成功~',
          })
        },
        fail: function (res) {
          wx.showToast({
            title: '点赞失败',
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: '大易益·传统文化大舞台，爱好者的掌中宝！',
      path: '/pages/index/index'
    }
  }
    // console.log(res)
    // if (res.from === 'button') {
    //   // 来自页面内转发按钮
      
    // }
    // var url = app.serverUrl+'addCoupon'
    // wx.request({
    //   url: url,
    //   method:'post',
    //   data:{
    //     openid: app.openid,
    //     couponid:this.data.couponid
    //   },
    //   success:function(res){
    //     console.log(res)
    //   }
    // })
    // return {
    //   title: '想睡觉了',
    //   path: '/pages/index/index'
    // }
  
})